#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N], b[N];
int c[635][N];
int n;

int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		for (int i = 1; i <= n; i++)scanf_s("%d", &a[i]);
		for (int i = 1; i <= n; i++)scanf_s("%d", &b[i]);
		int k = sqrt(2 * n);
		for (int i = 1; i <= n; i++) {
			if (a[i] <= k) c[a[i]][b[i]]++;
		}
		LL ans = 0;
		for (int i = 1; i <= n; i++) {
			if (a[i] <= k) {
				LL z = a[i] * a[i] - b[i];
				//cout << "z===" << z << endl;
				if (z >= 1 && z <= n) {
					ans += c[a[i]][z];
					//cout << "c==" << c[a[i]][z] << endl;
				}
				if (z == b[i])ans--;
			}
		}
		ans /= 2;
		for (int i = 1; i <= n; i++) {
			for (int x = 1; x <= k && x < a[i]; x++) {
				LL z = a[i] * x - b[i];
				//cout << "z==" << z << endl;
				if (z >= 1 && z <= n) {
					ans += c[x][z];
					//cout << "c==" << c[x][z] << endl;
				}
			}
		}
		printf("%lld\n", ans);
		for (int i = 1; i <= n; i++) {
			if (a[i] <= k)c[a[i]][b[i]]--;
		}
	}
	return 0;
}